#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Image.h"
#include "QImage"
#include<iostream>
using namespace std;



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("图像处理实验  黎泉林&莫文青");
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_pushButton_3_clicked()
{
    if(sharpen_valve%2==0)
    {
    sharpen_myimg=myimg;//拷贝副本
    myimg.sharpen();
    }
    else myimg=sharpen_myimg;

    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
    sharpen_valve++;

}

void MainWindow::on_pushButton_4_clicked()
{
    if(Grey_scale_valve%2==0)
    {
    Grey_scale_myimg=myimg;//拷贝副本
    myimg.Grey_scale();
    }
    else myimg=Grey_scale_myimg;

    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
    Grey_scale_valve++;
}

void MainWindow::on_actionopen_2_triggered()
{
    QString filename;
    filename=QFileDialog::getOpenFileName(this,
                                        tr("选择图像"),
                                        "",
                                        tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));//可打开的文件类型
  if(!filename.isEmpty())//判断文件路径是否为空，防止程序崩溃
  {
       if(myimg.ReadBMP(filename))
       {

        QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
        myimg.Secon_to_First(a.bits());
        ui->label->setPixmap(QPixmap::fromImage(a));

       }
  }
}

void MainWindow::on_actionsave_2_triggered()
{
    QString filename1 = QFileDialog::getSaveFileName(this,tr("请选择保存路径"),"",tr("Images ( *.bmp *.png *.jpg)")); //选择路径
   if(!filename1.isEmpty())
   {
    myimg.WriteBMP(filename1);
   }
}

void MainWindow::on_actiongongn_triggered()
{

}

void MainWindow::on_pushButton_clicked()
{
    if(sharpenre_valve%2==0)
    {
    sharpenre_myimg=myimg;//拷贝副本
    myimg.sharpenre();
    }
    else myimg=sharpenre_myimg;

    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));

    sharpenre_valve++;
}

void MainWindow::on_pushButton_5_clicked()
{
    myimg.Rotate(90);
    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
}

void MainWindow::on_pushButton_6_clicked()
{
    myimg.Rotate(-90);
    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
}

void MainWindow::on_pushButton_7_clicked()
{
    myimg.Flip(0);
    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
}

void MainWindow::on_pushButton_8_clicked()
{
    myimg.Flip(1);
    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
}

void MainWindow::on_pushButton_2_clicked()
{
    -myimg;
    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
}

void MainWindow::on_pushButton_11_clicked()
{
    Image myimg1;
    myimg1=myimg;
    myimg1.sharpenre();
    for(int i=0;i<myimg.GetHeight();i++)
    {
        for(int j=0;j<myimg.GetWidth()*3;j++)
        {
            myimg.SetDatare(i,j,myimg.GetData(i,j)/5);
        }
    }

    QImage a(myimg.GetWidth(),myimg.GetHeight(),QImage::Format_ARGB32);
    myimg.Secon_to_First(a.bits());
    ui->label->setPixmap(QPixmap::fromImage(a));
}
